/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.LoginBean;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.logic.db.AdminFunctionality;
import model.logic.db.UserFunctions;
import model.pojo.Admins;
import model.pojo.Users;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Maha
 */
public class LoginAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserFunctions uf = new UserFunctions();
        String msg = "";

        Users login = uf.login(((LoginBean) form).getEmail(), ((LoginBean) form).getPassword());
        if (login != null) {
            System.out.println("Here");
            HttpSession session = request.getSession(true);
            session.setAttribute("user", login);
            msg = "Welcome back, You will be redirected to HomePage after 10 seconds.";

            ((LoginBean) form).setMsg(msg);
        } else {
            //try login as admin,
            AdminFunctionality admin = new AdminFunctionality();
            Admins admLogin = admin.login(((LoginBean) form).getEmail(), ((LoginBean) form).getPassword());
            if (admLogin != null) {
                HttpSession session = request.getSession(true);
                session.setAttribute("admin", admLogin);
                msg = "Welcome back Admin, You will be redirected to Admin Form after 10 seconds.";
                ((LoginBean) form).setMsg(msg);

            } else {
                //===================
                msg = "Can't Login,Please check your username and password";
                ((LoginBean) form).setMsg(msg);
            }
        }
        return mapping.findForward("op");
    }
}
